

let NetPrompt = require('NetPrompt')

let mClient  = null

let httpSend = (cmd, data, callback, busy)=>{

    if (!mClient)
    {
        return
    }

    if (busy)
    {
        let nptId = NetPrompt.open()

        mClient.httpCmd(cmd, data, 
            (m)=>{

                NetPrompt.close(nptId)

                callback && callback(m)
            }
        )
    }
    else
    {
        mClient.httpCmd(cmd, data, callback)
    }
}

let NetProtocol =
{
    init()
    {
        if (mClient)
        {
            return
        }

        const url = require('App').configBy('serverAddress')

        // let NetClient = require('XXX')
        // mClient = new NetClient(url)
    },

    close()
    {
        mClient = null
    },

    time()
    {
        return mClient ? mClient.time() : cc.sys.now()
    },

    login(third, callback)
    {
        // mClient.updateSession(null)

        // let app = require('App'), p = require('ConstBase')

        // let cmd = 'user_login'

        // let d = {
        //     w: "123456",
        //     d: app.get(p.device_uid),
        //     p: app.platform(),
        //     c: app.get(p.channel),
        //     s: app.get(p.app_version),
        //     cou: app.get(p.country),
        //     lan: app.get(p.language),
        //     dn: app.get(p.device_name),
        //     ds: app.get(p.os_version),
        // }

        // if (third)
        // {
        //     d.t = third.id
        //     d.u = third.userId
        //     d.k = third.token
        // }

        // httpSend(cmd, d, 
        //     (m)=>{
                
        //         if (m && m.rs == 0)
        //         {
        //             mClient.updateSession(m.sn)
        //         }

        //         callback && callback(m)
        //     }
        // )

        callback && callback({ rs : 0})
    },

    logout(callback)
    {
        httpSend('user_logout', null, callback, true)
    },

    notice(callback)
    {
        httpSend('notice', null, callback)
    },

    mails(callback)
    {
        httpSend('mails', null, callback)
    },

    readMail(id, callback)
    {
        httpSend('readMail', {id:id}, callback)
    },
}

module.exports  = NetProtocol
